//
//  DatePickerView.m
//  DatePickerStudy
//
//  Created by ht on 16/6/13.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "DatePickerView.h"


@interface DatePickerView()


@property (nonatomic, strong) UIButton *bkView;


@property (nonatomic, assign) CGFloat vheight;

@end


@implementation DatePickerView


- (DatePickerView *)initWithCustomeHeight:(CGFloat)height
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height = height < 200 ? 200 : height)];
    if (self) {
        
        self.vheight  = height < 200 ? 200 : height;
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.05].CGColor;
        
        //创建取消 确定按钮
        UIButton *cannel = [UIButton buttonWithType:UIButtonTypeCustom];
        cannel.frame = CGRectMake(20, 0, 50, 40);
        [cannel setTitle:@"取消" forState:0];
        [cannel setTitleColor:[UIColor blackColor] forState:0];
        cannel.tag = 100;
        [cannel addTarget:self action:@selector(cannelOrConfirm:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cannel];
        
        
        UIButton *confirm = [UIButton buttonWithType:UIButtonTypeCustom];
        confirm.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-70, 0, 50, 40);
        [confirm setTitle:@"确定" forState:0];
        [confirm setTitleColor:[UIColor blackColor] forState:0];
        confirm.tag = 200;
        [confirm addTarget:self action:@selector(cannelOrConfirm:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:confirm];
        
        // 创建datapikcer
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, height - 40)];
        _datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _datePicker.date = [NSDate date];
        // 本地化
        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
        
        // 日期控件格式
//        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        _datePicker.datePickerMode = UIDatePickerModeDate;
        
        [self addSubview:_datePicker];

    }
    return self;
}

- (void)show
{
    _bkView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [_bkView addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
    _bkView.backgroundColor = [UIColor blackColor];
    _bkView.alpha = 0.6;
    [[UIApplication sharedApplication].keyWindow addSubview:_bkView];
    
    self.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.width - 240, [UIScreen mainScreen].bounds.size.width, self.vheight);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
}

- (void)disMiss
{
    [self removeFromSuperview];
    [_bkView removeFromSuperview];
}


//选择确定或者取消
- (void)cannelOrConfirm:(UIButton *)sender
{
    if (sender.tag==200) {
        
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"yyyy-MM-dd"];
        NSString *choseDateString = [dateformatter stringFromDate:_datePicker.date];
        
        if (self.confirmBlock) {
            self.confirmBlock(choseDateString);
        }

//        NSLog(@"intercalStr==%@",choseDateString);
        
    }
    if (self.cannelBlock) {
        self.cannelBlock();
    }
    [self disMiss];

}


@end
